import 'package:flutter/material.dart';

import '1.dart';

class ChatScreen extends StatelessWidget {
  final StreamApiService apiService = StreamApiService(
    apiUrl: 'https://api.openai.com/v1/chat/completions',
    apiKey: 'your-api-key',
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder<String>(
        stream: apiService.streamResponse("请给我讲个故事"),
        builder: (context, snapshot) {
          if (snapshot.hasError) {
            return Center(
              child: Text('Error: ${snapshot.error}'),
            );
          }

          if (!snapshot.hasData) {
            return Center(
              child: CircularProgressIndicator(),
            );
          }

          return SingleChildScrollView(
            child: Padding(
              padding: EdgeInsets.all(16.0),
              child: Text(snapshot.data ?? ''),
            ),
          );
        },
      ),
    );
  }
}